package com.common.sdk.net.download.db;

import com.common.sdk.net.download.util.DownloadLog;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DownloadSQLiteOpenHelper extends SQLiteOpenHelper {

    private static final String DOWNLOAD_DB = "download.db";

    private static final int VERSION = 3;
    private static DownloadSQLiteOpenHelper dBOpenHelper;
    private static Context context;

    private DownloadSQLiteOpenHelper(Context context) {
        super(context, DOWNLOAD_DB, null, VERSION);
    }

    public static void init(Context mContext) {
        context = mContext;
    }

    // public static DownloadSQLiteOpenHelper getIntance(Context context) {
    // if (dBOpenHelper == null) {
    // synchronized (DownloadSQLiteOpenHelper.class) {
    // if (dBOpenHelper == null) {
    // dBOpenHelper = new DownloadSQLiteOpenHelper(context);
    // }
    // }
    // }
    // return dBOpenHelper;
    // }

    public static DownloadSQLiteOpenHelper getIntance() {
        if (dBOpenHelper == null) {
            synchronized (DownloadSQLiteOpenHelper.class) {
                if (dBOpenHelper == null) {
                    dBOpenHelper = new DownloadSQLiteOpenHelper(context);
                }
            }
        }
        return dBOpenHelper;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建数据库
        db.execSQL(DownloadDetailesTables.getCreateFileDetailedTable());
        // db.execSQL(DownloadDetailesTables.getCreateThreadDetailed());
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        DownloadLog.debug("onUpgrade:" + oldVersion + "," + newVersion);
        // 理论上没有相等的情况
        if (oldVersion >= newVersion) {
            return;
        }

        upgradeVersion(db, oldVersion, newVersion);
    }

    /**
     * 对各个版本的数据库采取不同的升级策略
     * 
     * @param db 数据库对象
     */
    private void upgradeVersion(SQLiteDatabase db, int oldVersion, int newVersion) {
        for (int index = oldVersion + 1; index <= newVersion; index++) {
            updateVersion(db, oldVersion, index);
        }
    }

    /**
     * 升级到某个版本。跨版本升级时，会逐级升级到每个版本，直到最高版本。<br />
     * 比如版本1升级到版本4:<br />
     * 1>2>3>4
     * 
     * @param db 数据库对象
     * @param version 数据库将要升级到的版本号
     */
    private void updateVersion(SQLiteDatabase db, int oldVersion, int version) {
        switch (version) {
            case 2:// 增加后缀名数据库存储
                db.execSQL(sqlColumnADD(DownloadDetailesTables.DOWNLOAD_DB_FILE_DETAILE,
                        DownloadDetailesTables.SUFFIX_NAME, "char"));
                break;
            case 3:// 增加后缀名数据库存储
                db.execSQL(sqlColumnADD(DownloadDetailesTables.DOWNLOAD_DB_FILE_DETAILE,
                        DownloadDetailesTables.DOWNLOAD_SOURCE, "integer"));
                break;

        }
    }

    /**
     * 获得添加列名的语句
     * 
     * @param columnName
     * @return
     */
    private String sqlColumnADD(String tableName, String columnName, String otherCondition) {
        return "ALTER TABLE " + tableName + "  ADD COLUMN  [" + columnName + "] " + otherCondition;
    }
}
